package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.pojo.entity.Score;
import com.example.teachingSystem.pojo.entity.SysUser;
import com.example.teachingSystem.pojo.form.score.AddScoreForm;
import com.example.teachingSystem.pojo.form.score.ScorePageQuery;
import com.example.teachingSystem.pojo.form.score.UpdateScoreForm;
import com.example.teachingSystem.pojo.vo.ScoreInfoVo;
import com.example.teachingSystem.pojo.vo.UserInfoVo;
import com.example.teachingSystem.service.ScoreService;
import com.example.teachingSystem.mapper.ScoreMapper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86147
* @description 针对表【score(成绩表)】的数据库操作Service实现
* @createDate 2023-11-27 21:43:54
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
    implements ScoreService{

    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public Result<String> addScore(AddScoreForm addScoreForm) {
        Score score = new Score();
        BeanUtils.copyProperties(addScoreForm,score);
        if(this.save(score)){
            return Result.ok(null,"添加成功");
        }
        return Result.build(null,500,"操作失败");
    }

    @Override
    public Result<String> updateScore(UpdateScoreForm updateScoreForm) {
        Score score = scoreMapper.selectOne(new LambdaQueryWrapper<Score>()
                .eq(Score::getScoreId,updateScoreForm.getScoreId()));
        if(score == null){
            return Result.build(null,"成绩不存在");
        }
        BeanUtils.copyProperties(updateScoreForm,score);
        scoreMapper.updateById(score);
        return Result.ok(null,"修改成功");
    }

//    @Override
//    public Result<String> deleteScoreByScoreId(Long scoreId) {
//        scoreMapper.deleteScoreByScoreId(scoreId);
//        return Result.ok(null,"删除成功");
//    }

    @Override
    public Result<String> deleteScoreByScoreId(Long scoreId) {
        Score score = scoreMapper.selectOne(new LambdaQueryWrapper<Score>()
                .eq(Score::getScoreId,scoreId));
        if(score == null){
            return Result.build(null,"成绩不存在");
        }
        scoreMapper.deleteById(scoreId);
        return Result.ok(null,"删除成功");
    }

    @Override
    public PageResult<ScoreInfoVo> showScoresByAdmin(ScorePageQuery scorePageQuery) {
        System.out.println("pageNum:"+scorePageQuery.getPageNum()+",pageSize:"+scorePageQuery.getPageSize());
        if(scorePageQuery.getKeywords()!=null){
            System.out.println("keywords:"+scorePageQuery.getKeywords());
        }
        Page<Score> scorePage = new Page<>(scorePageQuery.getPageNum(), scorePageQuery.getPageSize());
        IPage<ScoreInfoVo> scoreInfoVoIPage = scoreMapper.showScoresByAdmin(scorePage, scorePageQuery);
        return PageResult.success(scoreInfoVoIPage);
    }

    @Override
    public PageResult<ScoreInfoVo> showScoresByTeacher(ScorePageQuery scorePageQuery) {
        Long teacherId = scorePageQuery.getId();
        System.out.println("teacher id:"+teacherId);
        if(teacherId != null){
            System.out.println("pageNum:"+scorePageQuery.getPageNum()+",pageSize:"+scorePageQuery.getPageSize());
            Page<Score> scorePage = new Page<>(scorePageQuery.getPageNum(), scorePageQuery.getPageSize());
            IPage<ScoreInfoVo> scoreInfoVoIPage = scoreMapper.showScoresByTeacher(scorePage, scorePageQuery);
            if(scorePageQuery.getKeywords()!=null){
                System.out.println("keywords:"+scorePageQuery.getKeywords());
            }
            return PageResult.success(scoreInfoVoIPage);
        }
        return PageResult.build(null,500,"查询参数不完整");
    }

    @Override
    public PageResult<ScoreInfoVo> showScoresByStudent(ScorePageQuery scorePageQuery) {
        Long studentId = scorePageQuery.getId();
        System.out.println("student id:"+studentId);
        if(studentId != null){
            System.out.println("pageNum:"+scorePageQuery.getPageNum()+",pageSize:"+scorePageQuery.getPageSize());
            Page<Score> scorePage = new Page<>(scorePageQuery.getPageNum(), scorePageQuery.getPageSize());
            if(scorePageQuery.getKeywords()!=null){
                System.out.println("keywords:"+scorePageQuery.getKeywords());
            }
            IPage<ScoreInfoVo> scoreInfoVoIPage = scoreMapper.showScoresByStudent(scorePage, scorePageQuery);
            return PageResult.success(scoreInfoVoIPage);
        }
        return PageResult.build(null,500,"查询参数不完整");
    }
}




